#include <stdio.h>
#include <stdlib.h>
struct node
{
    int xishu;
    int zhishu;
    struct node *next;
};
typedef struct node N, *List;
List read()
{
    int n;
    List p, tail, head;
    head = (List)malloc(sizeof(N));
    tail = head;
    head->next = NULL;
    scanf("%d", &n);
    while(n--)
    {
        p = (List)malloc(sizeof(N));
        scanf("%d %d", &p->xishu, &p->zhishu);
        tail->next = p;
        tail = p;
        tail->next = NULL;
    }
    tail = head;
    head = head->next;
    free(tail);
    return head;
}
List addition(List l1, List l2)
{
    List head, p, tail;
    head = (List)malloc(sizeof(N));
    tail = head;
    while(l1&&l2)
    {
        if(l1->zhishu>l2->zhishu)
        {
            p = (List)malloc(sizeof(N));
            p->xishu = l1->xishu;
            p->zhishu = l1->zhishu;
            tail ->next = p;
            tail = p;
            tail->next = NULL;
            l1 = l1->next;
        }
        else if(l1->zhishu<l2->zhishu)
        {
            p = (List)malloc(sizeof(N));
            p->xishu = l2->xishu;
            p->zhishu = l2->zhishu;
            tail ->next = p;
            tail = p;
            tail->next = NULL;
            l2 = l2->next;
        }
        else
        {
            int sum;
            sum = l1->xishu + l2->xishu;
            if(sum)
            {
                p = (List)malloc(sizeof(N));
                p->xishu = sum;
                p->zhishu = l1->zhishu;
                tail ->next = p;
                tail = p;
                tail ->next = NULL;
            }
            l1 = l1->next;
            l2 = l2->next;
        }
    }
    while(l1)
    {
        p = (List)malloc(sizeof(N));
        p->xishu = l1->xishu;
        p->zhishu = l1->zhishu;
        tail ->next = p;
        tail = p;
        tail->next = NULL;
        l1 = l1->next;
    }
    while(l2)
    {
        p = (List)malloc(sizeof(N));
        p->xishu = l2->xishu;
        p->zhishu = l2->zhishu;
        tail ->next = p;
        tail = p;
        tail->next = NULL;
        l2 = l2->next;
    }
    tail = head;
    head = head->next;
    free(tail);
    return head;
}
List hello(List l, int xishu, int zhishu)
{
    int flog=1;
    List h, p;
    h = l;
    while(h->next)
    {
        if(h->next->zhishu<zhishu)
        {
            p = (List)malloc(sizeof(N));
            p->xishu = xishu;
            p->zhishu = zhishu;
            p->next = h->next;
            h->next = p;
            break;
        }
        else if((h->next->zhishu) == zhishu)
        {

            if((h->next->xishu+xishu)==0)
            {
                p = h->next;
                h->next = p->next;
                free(p);
            }
            else {
                h->next->xishu += xishu;
            }
            flog=0;
            break;


        }
        h = h->next;

    }
    if(h->next==NULL&&flog!=0)
    {
        p = (List)malloc(sizeof(N));
        p->xishu = xishu;
        p->zhishu = zhishu;
        p->next = h->next;
        h->next = p;
    }
    return l;
}
List multiplication(List l1, List l2)
{
    List ll = NULL;
    if(!l1||!l2)
        return ll;
    List head, tail, p, l3 = l2;
    head = (List)malloc(sizeof(N));
    tail = head;
    head ->next = NULL;
    while(l2)
    {
        p = (List)malloc(sizeof(N));
        p->xishu = l1->xishu * l2->xishu;
        p->zhishu = l1->zhishu + l2->zhishu;
        p->next = NULL;
        tail ->next = p;
        tail = p;
        l2 = l2->next;
    }
    l1 = l1->next;
    l2 = l3;
    while(l1)
    {
        while(l2)
        {
            int xishu, zhishu;
            xishu = l1->xishu*l2->xishu;
            zhishu = l1->zhishu+l2->zhishu;
            head = hello(head, xishu, zhishu);
            l2 = l2->next;
        }
        l1 = l1->next;
        l2 = l3;
    }
    tail = head;
    head = head->next;
    free(tail);
    return head;
}

int main()
{
    List l1 = NULL, l2 = NULL, p;
    l1 = read();
    l2 = read();
    if(!multiplication(l1, l2))
        printf("0 0");
    for(p = multiplication(l1, l2); p!=NULL; p = p->next)
    {
        if(p->next == NULL)
            printf("%d %d", p->xishu, p->zhishu);
        else
            printf("%d %d ", p->xishu, p->zhishu);
    }
    printf("\n");
    if(!addition(l1, l2))
        printf("0 0");
    for(p = addition(l1, l2); p!=NULL; p = p->next)
    {
        if(p->next == NULL)
            printf("%d %d", p->xishu, p->zhishu);
        else
            printf("%d %d ", p->xishu, p->zhishu);
    }
    return 0;
}
